using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class MemberComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private SpriteFont _menuFont, _textFont;
        private Vector2 touchPosition, pretouchPosition;
        private int _number;
        private Button _numButton;

        List<Button> _listItemMenu;
        public List<Button> ListItemMenu
        {
            get { return _listItemMenu; }
            set { _listItemMenu = value; }
        }

        private List<Button> _listAHero;
        public List<Button> ListAHero
        {
            get { return _listAHero; }
            set { _listAHero = value; }
        }

        //Initilation
        public MemberComponent(Game game, SpriteFont menuFont, SpriteFont textFont, Texture2D textureStartGame, List<Texture2D> listIconAHero, SpriteBatch spriteBatch)
            : base(game)
        {
            _game = game;
            _menuFont = menuFont;
            _textFont = textFont;
            _spriteBatch = spriteBatch;

            _number = 3;
            //Init list menu item
            ListItemMenu = new List<Button>();
            ListItemMenu.Add(new Button("Back", _textFont, new Vector2(690, 400), Color.Red));
            ListItemMenu.Add(new Button(textureStartGame, new Vector2(20, 0)));
            _numButton = new Button("", _menuFont, new Vector2(320, 80), Color.White) { HaveAnimation = false };
            _listAHero = new List<Button>();
            _listAHero.Add(new Button(listIconAHero[0],new Vector2(50, 190)));
            _listAHero.Add(new Button(listIconAHero[1], new Vector2(170, 190)));
            _listAHero.Add(new Button(listIconAHero[2], new Vector2(290, 190)));
            _listAHero.Add(new Button(listIconAHero[3], new Vector2(410, 190)));
            _listAHero.Add(new Button(listIconAHero[4], new Vector2(170, 330)));
            _listAHero.Add(new Button(listIconAHero[5], new Vector2(290, 330)));
            _listAHero.Add(new Button(listIconAHero[6], new Vector2(410, 330)));
            for (int i = 0; i < _listAHero.Count; i++)
            {
                _listAHero[i].Selected += new Button.IsSelected(Member_Selected);
                _listAHero[i].ColorTexture = Color.Gray;
            }
        }

        void Member_Selected(int input)
        {
            if (_listAHero[input].ColorTexture == Color.Gray)
            {
                if (_number > 0)
                {
                    _listAHero[input].ColorTexture = Color.White;
                    for (int i = 0; i < ManagerGame.luffy.LisAHeronow.Count; i++)
                        if (ManagerGame.luffy.LisAHeronow[i] == -1)
                        {
                            ManagerGame.luffy.LisAHeronow[i] = input;
                            break;
                        }
                    _number--;
                }
            }
            else
            {
                _listAHero[input].ColorTexture = Color.Gray;
                for (int i = 0; i < ManagerGame.luffy.LisAHeronow.Count; i++)
                    if (ManagerGame.luffy.LisAHeronow[i] == input)
                    {
                        ManagerGame.luffy.LisAHeronow[i] = -1;
                        ManagerGame.luffy.LisAHeronow.Sort();
                        ManagerGame.luffy.LisAHeronow.Reverse();
                        break;
                    };
                _number++;
            }
            
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            /*//Key input
            TouchCollection touchCollection = TouchPanel.GetState();
            touchPosition = Vector2.Zero;
            if (touchCollection.Count > 0)
            {
                touchPosition = touchCollection[0].Position;
                pretouchPosition = touchPosition;
            }*/           

            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    touchPosition = gesture.Position;                    
                    for (int i = 0; i < ManagerGame.luffy.ListAHeroHave.Count; i++)
                        _listAHero[ManagerGame.luffy.ListAHeroHave[i]].ButtonIsTouched(touchPosition, ManagerGame.luffy.ListAHeroHave[i]);
                    for (int i = 0; i < ListItemMenu.Count; i++)
                        ListItemMenu[i].ButtonIsTouched(touchPosition, i);
                    
                }
            }

            _numButton.Text = _number.ToString();
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            for (int i = 0; i < ListItemMenu.Count; i++)
                ListItemMenu[i].Draw(_spriteBatch, gameTime);
            for (int i = 0; i < ManagerGame.luffy.ListAHeroHave.Count; i++)
                _listAHero[ManagerGame.luffy.ListAHeroHave[i]].Draw(_spriteBatch, gameTime);
            _numButton.Draw(_spriteBatch, gameTime);
        }
    }
}
